import type { Customer } from "../types/user";
import type { MeetingRoom } from "../types/room";
import { request } from "@/service/request/request";
import type { Reservation, Cancellation, Order } from "../types/reservation";

/**
 * 客户获取自己的预约单列表
 * @param customerId
 * @param page
 * @param size
 * @returns
 */
export const getReservationsByCustomerId = async (
    customerId: string,
    page: number,
    size: number
) => {
    try {
        return await request<Reservation[]>(
            `/order/get_page_reservation_byCustomerId/${customerId}`,
            "post",
            {
                page: page,
                size: size,
            },
            { loading: false }
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 根据状态获取预约单数量
 * @param status 0:已审核 1:未审核
 * @returns
 */
export const getReservationCountsByStatus = async (status: 0 | 1) => {
    try {
        return await request<number>(
            `/order/get_all_reservation_Count/${status}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

export const getReservationById = async (reservationId: string) => {
    try {
        return await request<Reservation>(
            `/order/get_reservation_by_id/${reservationId}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 根据状态分页获取预约单
 * @param status 0:已审核 1:未审核
 * @param page
 * @param size
 * @returns
 */
export const getReservationsByStatus = async (
    status: 0 | 1,
    page: number,
    size: number
) => {
    try {
        return await request<Reservation[]>(
            `/order/get_page_reservation/${status}`,
            "post",
            { page, size }
        );
    } catch (error) {
        throw error;
    }
};

export const getReservationDetail = async (reservationId: string) => {
    try {
        return await request<{
            order: Order;
            customer: Customer;
            meetingRoom: MeetingRoom;
        }>(
            `/order/getReservation_relate_detail/${reservationId}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 员工删除预约单，同时会删除对应的订单
 * @param reservationId
 * @returns
 */
export const deleteReservation = async (reservationId: string) => {
    try {
    } catch (error) {
        throw error;
    }
};

/**
 * 员工审核预约单
 * @param reservationId
 * @param status
 * @returns
 */
export const auditReservation = async (
    reservationId: string,
    status: "rejected" | "confirmed"
) => {
    try {
        return await request<null>("/order/audit_reservation", "post", {
            reservationId: reservationId,
            status: status,
        });
    } catch (error) {
        throw error;
    }
};

/**
 * 客户取消预订,提交取消预定表单
 * @param cancellation
 * @returns
 */
export const cancelReservation = async (cancellation: Cancellation) => {
    try {
        return await request<null>(
            "/order/submit_cancel_reservation",
            "post",
            cancellation
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 根据审核状态获取取消订单数量
 * @param status 0:已审核 1:未审核
 * @returns
 */
export const getCancellationCountsByStatus = async (status: 0 | 1) => {
    try {
        return await request<number>(
            `/order/get_all_cancellation_Count/${status}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 根据审核状态分页获取取消订单
 * @param status 0:已审核 1:未审核
 * @param page
 * @param size
 * @returns
 */
export const getCancellationsByStatus = async (
    status: 0 | 1,
    page: number,
    size: number
) => {
    try {
        return await request<Cancellation[]>(
            `/order/get_page_cancellation/${status}`,
            "post",
            { page, size }
        );
    } catch (error) {
        throw error;
    }
};

export const auditCancellation = async (
    cancellationId: string,
    status: "rejected" | "approved"
) => {
    try {
        return await request<null>("/order/audit_cancellation", "post", {
            cancellationId: cancellationId,
            status: status,
        });
    } catch (error) {
        throw error;
    }
};
